import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

class Whisper {
  /// 轻声提示（成功提示）
  static void murmur(String message, {Duration? duration}) {
    SmartDialog.showToast(
      message,
      displayTime: duration ?? const Duration(seconds: 2),
      alignment: Alignment.center,
      builder:
          (_) => _buildToastWidget(
            message: message,
            backgroundColor: const Color(0xFF4CAF50),
            icon: Icons.check_circle_outline,
          ),
    );
  }

  /// 警告低语（警告提示）
  static void hiss(String message, {Duration? duration}) {
    SmartDialog.showToast(
      message,
      displayTime: duration ?? const Duration(seconds: 2),
      alignment: Alignment.center,
      builder:
          (_) => _buildToastWidget(
            message: message,
            backgroundColor: const Color(0xFFFFA726),
            icon: Icons.warning_amber_rounded,
          ),
    );
  }

  /// 尖叫警告（错误提示）
  static void shriek(String message, {Duration? duration}) {
    SmartDialog.showToast(
      message,
      displayTime: duration ?? const Duration(seconds: 2),
      alignment: Alignment.center,
      builder:
          (_) => _buildToastWidget(
            message: message,
            backgroundColor: const Color(0xFFF44336),
            icon: Icons.error_outline,
          ),
    );
  }

  /// 低声耳语（普通信息提示）
  static void breath(String message, {Duration? duration}) {
    SmartDialog.showToast(
      message,
      displayTime: duration ?? const Duration(seconds: 2),
      alignment: Alignment.topCenter,
      builder:
          (_) => _buildToastWidget(
            message: message,
            backgroundColor: const Color(0xFF2196F3),
            icon: Icons.info_outline,
          ),
    );
  }

  /// 忙碌咒语（加载中）
  static void chant({String? message}) {
    SmartDialog.showLoading(
      builder: (_) => _buildLoadingWidget(message: message),
    );
  }

  /// 结束咒语（关闭加载中）
  static void silence() {
    SmartDialog.dismiss();
  }

  /// 神秘对话框（自定义对话框）
  static Future<T?> portal<T>({
    required Widget child,
    bool clickMaskDismiss = true,
    Alignment alignment = Alignment.center,
  }) async {
    return SmartDialog.show<T>(
      clickMaskDismiss: clickMaskDismiss,
      alignment: alignment,
      builder: (_) => child,
    );
  }

  /// 构建Toast小部件
  static Widget _buildToastWidget({
    required String message,
    required Color backgroundColor,
    required IconData icon,
  }) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 12.h),
      margin: EdgeInsets.only(top: 56.h, left: 16.w, right: 16.w),
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(8.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.2),
            blurRadius: 8.r,
            spreadRadius: 1.r,
          ),
        ],
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, color: Colors.white, size: 20.sp),
          SizedBox(width: 10.w),
          Flexible(
            child: Text(
              message,
              style: TextStyle(
                color: Colors.white,
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 构建加载中小部件
  static Widget _buildLoadingWidget({String? message}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 16.h),
      decoration: BoxDecoration(
        color: Colors.black.withOpacity(0.7),
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
          ),
          if (message != null) ...[
            SizedBox(height: 12.h),
            Text(
              message,
              style: TextStyle(color: Colors.white, fontSize: 14.sp),
            ),
          ],
        ],
      ),
    );
  }
}
